<?php
  require_once('./db.php');
  require_once('./header.php');

  $msg = '';
  $flag = true;
pr($_SESSION);

  if(isset($_POST['name']) && !empty($_POST['name']))
  {
    if($_POST['captcha'] == $_SESSION['secretword'])
    {
      unset($_POST['captcha']);
      $_POST['added_date'] = date('Y-m-d H:i:s');
      $_POST['status'] = '1';
      pr($_POST);
      $_POST = arrayCleanup($_POST);
      $_POST['password'] = md5($_POST['password']);

      ###############################
      // $rules is in db.php
      $uploadArray = uploadFile('avatar',$rules);

      $renamedName =  $uploadArray['renamedName'];
      $msg =  $uploadArray['msg'];
      $flag =  $uploadArray['flag'];
      if($flag==true) {
        $_POST['avatar'] = $renamedName;

        require_once('thumbnail/image.class.php');
        $img = new Zubrag_image;
        $thumb_dir = './uploads/thumbs';
        if(!is_dir($thumb_dir)) {
          mkdir($thumb_dir,0755);
        }
        $img->max_x        = 100;
        $img->max_y        = 100;

        $img->GenerateThumbFile("./uploads/$renamedName", "$thumb_dir/$renamedName");
      }
      ###############################
      if($flag == true)
      {
        #####################################
        $id = dbInsert('users',$_POST);
        #####################################
        $msg =  "Record inserted on ID <strong></strong>$id</strong>";
        header("location:display_users.php");
        exit;
      }
    } else {
        $msg =  "Security Code didn't match.";
    }
  }// end if POST check

  if($msg != '') {
    echo '<h4 style="background:yellow;border:2px solid pink;"><em>'.$msg.'</em></h4>';
  }
?>
<h1 class="page-header">Add New User</h1>
<form action="" method="post" enctype="multipart/form-data">
  <table class="table table-bordered">
    <thead>
      <tr>
        <th colspan="2">Please add user</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td align="right">Name :</td>
        <td><input required placeholder="User Name" value="<?=@$_POST['name']?>" type="text" name="name"/></td>
      </tr>
      <tr>
        <td align="right">Email :</td>
        <td><input required value="<?=@$_POST['email']?>"  placeholder="Email"  type="email" name="email"/></td>
      </tr>
      <tr>
        <td align="right">Password :</td>
        <td><input required placeholder="Password"  type="password" name="password"/></td>
      </tr>
      <tr>
        <td align="right">Avatar :</td>
        <td><input required type="file" name="avatar"/></td>
      </tr>
      <tr>
        <td align="right">Security Code :</td>
        <td>
<!-- Captcha Start -->
<img src="captcha/captcha.php" id="captcha" /><br/>
<a href="javascript:void(0)" onclick="    document.getElementById('captcha').src='captcha/captcha.php?'+Math.random();
    document.getElementById('captcha-form').focus();"
    id="change-image">Not readable? Change text.</a><br/><br/>
<input type="text" placeholder="please enter code" name="captcha" autocomplete="Off" required id="captcha-form" /><br/>
<!-- Captcha End-->


        </td>
      </tr>


      <tr>
        <td align="right">&nbsp;</td>
        <td><input type="submit" value="I am done!!" /></td>
      </tr>
    </tbody>
  </table>

</form>
<?php
  require_once('footer.php');
?>
